{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Binding</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            padding: 20px;
        }

        .container {
            max-width: 400px;
            margin: 0 auto;
            background-color: #fff;
            padding: 40px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, .1);
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        label {
            display: block;
            font-weight: bold;
            margin-bottom: 10px;
        }

        input[type="email"], input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            background-color: #4CAF50;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
    <script>
        function getVerificationCode() {
            // 在这里添加获取验证码的逻辑
            alert("验证码已发送至您的邮箱！");
        }
    </script>
</head>
<body>
<div class="container">
    <h2>绑定用户</h2>
    <form id="userForm" action="" method="post">
        {% csrf_token %}
        <label for="access_token"></label>
        <input style="display: none" id="access_token" name="access_token" value="{{ access_token }}">

        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>

        <label for="password">password:</label>
        <input type="password" id="password" name="password" required>
        <span id="error_email" style="color: red;display: none"></span>
        <button type="button" id="send_email">获取验证码</button>

        <label for="verificationCode">Verification Code:</label>
        <input type="text" id="verificationCode" name="email_code" required>

        <button type="submit">绑定</button>
    </form>
</div>

<script src="{% static 'js/jquery-3.7.0.min.js' %}"></script>
<script src="{% static 'js/axios-0.18.0.min.js' %}"></script>
<script>
    $(document).ready(function () {
        $('#send_email').click(function () {
            var email_ele = $('#email')
            var email = email_ele.val()
            var re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
            var error_ele = $('#error_email')
            if (!re.test(email)) {

                error_ele.text('邮箱格式错误')
                error_ele.show()
                return;
            } else {
                error_ele.hide()
            }
            let url = '/email_codes/' + email + '/';
            axios.get(url, {
                responseType: 'json'
            })
                .then(response => {
                    if (response.data.code === '0') {
                        // 展示倒计时60秒
                        let num = 60;

                        // setInterval('回调函数', '时间间隔(毫秒)')
                        let t = setInterval(() => {
                            if (num === 1) {
                                // 停止回调函数
                                clearInterval(t);
                                $(this).text('获取邮箱验证码')
                                this.send_flag = false;

                            } else {
                                num -= 1;
                                $(this).text(num + '秒')
                            }

                        }, 1000)
                    } else {
                        err_sendemail_ele.text('访问过于频繁')
                        err_sendemail_ele.show()
                    }

                })
                .catch(error => {
                    console.log(error.response);

                })
        })
    })
</script>
</body>
</html>
